﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin
{
    public class BaseAdmin : System.Web.UI.Page
    {

        readonly DALAdminLogin theAdminLogin = new DALAdminLogin();
        readonly UserRuleRef theUserRuleRef = new UserRuleRef();
        readonly dalCompany theCompany = new dalCompany();
        readonly DALRule theRule = new DALRule();
        public DB_adminlogin LoginAdminUser
        {
            get
            {
                if (this.Session["Adminlogin"] != null)
                {
                    return this.Session["Adminlogin"] as DB_adminlogin ;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.Session["AdminLogin"] = value;
            }
        }


        public string CompanyId
        {
            get
            {
                if (this.Session["CompanyId"] != null)
                {
                    return this.Session["CompanyId"].ToString();
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.Session["CompanyId"] = value;
            }
        }


        protected void Page_Init(object sender, EventArgs e)
        {
            if ( LoginAdminUser== null)
            {
                base.Response.Redirect("login.aspx");
            }
        }

        public void ValidateLoginRule(ERule oRule)
        {
           
           DataSet ds;
           bool bValidate = false;
           ds = theUserRuleRef.GetListByAdminId(LoginAdminUser.AdminId);
           if (ds != null && ds.Tables.Count > 0)
           {
               foreach (DataRow dr in ds.Tables[0].Rows)
               {
                   if (dr["Name"].ToString() == oRule.ToString())
                   {
                       bValidate = true;
                       break;
                   }
               }
           }
           if (!bValidate)
           {
               base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
               base.Response.End();
           }
        }

       void ValidatePageLoginRule(ERule oPageRule)
        {
         
            tbCompany oCompany = theCompany.GetModelByAdminId(LoginAdminUser.AdminId);
            bool isValidatePage = false;
            DataSet ds;
            ds = theUserRuleRef.GetListByAdminId(LoginAdminUser.AdminId);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
                    if (oERule == oPageRule)
                    {
                        break;
                    }
                }
            }
            if (!isValidatePage)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }

        }

    }
}